// pages/order_detail/order_detail.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        title: "订单详情",
        order_cancle: false,
        order_cancle1: false,
        code_confirm: false,
        code_can: false,
        top: "",
        detail: {},
        componentStyle: {
            navigationTitle: "订单详情",
            goBack: true
        },
        status: 0
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.getBase();
        this.setData({
            order_sn: options.order_sn,
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getData();
        if (options.status) {
            this.setData({
                status: options.status
            });
        }
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                var componentStyle = that.data.componentStyle;
                that.setData({
                    componentStyle: {
                        ...componentStyle,
                        color_minor: !res.extra.color_main ? "#FE513B" : res.extra.color_main,
                        color_minor: !res.extra.color_minor ? "#FE513B" : res.extra.color_minor
                    },
                    timeStyle: {
                        height: 32,
                        mainColor: res.extra.color_main
                    },
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style
                });
            }
        });
    },
    getData: function() {
        let that = this;
        common.request({
            endpoint: "Localbusiness/order/order-detail",
            method: "post",
            data: {
                order_sn: that.data.order_sn
            },
            success: res => {
                if (res.extra.product_content.using_expire_in) {
                    var using_expire_in = res.extra.product_content.using_expire_in;
                    res.extra.product_content.using_expire_in = using_expire_in ? res.extra.product_content.using_expire_in.split(",").join("~") : "永久有效";
                }
                if (res.extra.status == 4 || res.extra.status == 5) {
                    res.extra.write_off_code.map(v => {
                        v.used = "1";
                    });
                }
                that.setData({
                    detail: res.extra
                });
            }
        });
    },
    cancel_order: function() {
        this.setData({
            order_cancle: true
        });
    },
    close_confirm: function() {
        this.setData({
            order_cancle: false
        });
    },
    confirm_order: function() {
        let that = this;
        common.request({
            endpoint: "Localbusiness/order/cancel-order",
            method: "post",
            data: {
                order_sn: that.data.order_sn
            },
            success: res => {
                that.setData({
                    order_cancle: res.extra
                });
                wx.navigateTo({
                    url: "/localLifePackage/pages/order/order"
                });
            }
        });
    },
    confirmPay: function() {
        if (this.data.detail.local_type == "1") {
            this.pay();
        } else {
            this.pay();
        }
    },
    pay: function() {
        let that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Localbusiness/order/pay-at-once",
            method: "post",
            data: {
                openid: openid,
                order_sn: that.data.order_sn
            },
            success: res => {
                console.log(res);
                that.payWay(res.extra);
            }
        });
    },
    code_cancel: function() {
        this.setData({
            code_confirm: false
        });
    },
    order_confirm: function(e) {
        let item = e.currentTarget.dataset;
        if (item.code == 0) {
            common.request({
                endpoint: "Localbusiness/order/order-write-off",
                method: "post",
                data: {
                    order_sn: that.data.order_sn,
                    code: item.code
                },
                success: res => {}
            });
        } else {}
    },
    codeApply: function() {
        this.setData({
            code_confirm: true
        });
        let query = wx.createSelectorQuery();
        let that = this;
        query.select("#code_apply").boundingClientRect(rect => {
            let clientHeight = rect.height;
            let clientWidth = rect.width;
            let ratio = 350 / clientWidth;
            that.setData({
                top: clientHeight * ratio * 2 + 350 + "rpx"
            });
        }).exec();
    },
    code_confirm: function() {
        this.setData({
            code_can: false
        });
    },
    /**
   * 
   * 微信支付
   * 
   */
    payWay(res) {
        let that = this;
        var order_id = res.order_id;
        wx.requestPayment({
            timeStamp: res.timeStamp,
            nonceStr: res.nonceStr,
            package: res.package,
            signType: res.signType,
            paySign: res.paySign,
            success: function(res) {
                console.log(res);
                wx.showToast({
                    title: "支付成功",
                    icon: "success",
                    mask: true,
                    duration: 3e3,
                    success: function(res) {
                        wx.showToast({
                            title: "购买成功！",
                            icon: "success",
                            success: function() {
                                if (that.data.detail.local_type == 1) {
                                    wx.reLaunch({
                                        url: `/localLifePackage/pages/reward/reward?id=${order_id}`
                                    });
                                } else {
                                    wx.reLaunch({
                                        url: "/localLifePackage/pages/order/order"
                                    });
                                }
                            }
                        });
                    }
                });
            },
            fail: function(res) {
                wx.navigateTo({
                    url: "/localLifePackage/pages/order/order"
                });
            }
        });
    },
    close_confirm1: function() {
        this.setData({
            order_cancle1: false
        });
    },
    cancel_order2: function() {
        this.setData({
            order_cancle1: true
        });
    },
    confirm_order1: function() {
        let that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Localbusiness/order/order-refund",
            method: "post",
            data: {
                openid: openid,
                order_sn: that.data.order_sn
            },
            success: res => {
                console.log(res);
                that.setData({
                    order_cancle1: false
                });
                if (res.extra == 0) {
                    //核销成功
                    wx.showModal({
                        title: "提示",
                        content: "提交取消成功，耐心等待审核!",
                        showCancel: false
                    });
                } else if (res.extra == 3) {
                    //退款审核中
                    wx.showModal({
                        title: "提示",
                        content: "退款审核中，请耐性等待",
                        showCancel: false
                    });
                } else if (res.extra == 1) {
                    //退款失败
                    wx.showModal({
                        title: "提示",
                        content: "该订单退款失败",
                        showCancel: false
                    });
                } else if (res.extra == 2) {
                    //非待核销的订单不可申请退款
                    wx.showModal({
                        title: "提示",
                        content: "非待核销的订单不可申请退款",
                        showCancel: false
                    });
                } else if (res.extra == 4) {
                    //已经退款
                    wx.showModal({
                        title: "提示",
                        content: "该订单已经退款",
                        showCancel: false
                    });
                }
            },
            fail(err) {
                that.setData({
                    order_cancle1: false
                });
                wx.showModal({
                    title: "提示",
                    content: err.message,
                    showCancel: false
                });
            }
        });
    }
});